﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PBDesk.Utilities;
using Umbraco.Cms.Web;

namespace PBDesk.com.Extension.Articles
{
    public class ArticleHelper
    {
        public static OArticle GetArticleOfTheDay(dynamic model, int artId = 0)
        {
            OArticle retArt = null;
            dynamic artOfDay = null;
            if (artId == 0)
            {
                artId = GetCurrentArticleId();
            }
            while (artOfDay == null && artId > 20110101)
            {
                var arts = model.AncestorsOrSelf.Last().ArticlePages.Where("articleId == @0", artId.ToString());
                if (arts != null)
                {
                    artOfDay = arts.FirstOrDefault();
                }
                artId--;
            }
            if (artOfDay != null)
                retArt = GetArticleFromBendyObject(artOfDay);
            return retArt;
        }

        public static OArticle GetArticleFromBendyObject(dynamic artOfDay)
        {
            OArticle retArt = null;
            if (artOfDay != null)
            {
                retArt = new OArticle();
                retArt.ArtId = artOfDay.articleId;
                retArt.MetaTitle = artOfDay.metaTitle ?? string.Empty;
                retArt.Title = artOfDay.itemTitle;
                retArt.Description = artOfDay.itemDescription;
                retArt.Link = artOfDay.itemLink;
                retArt.ShortUrl = artOfDay.articleShortUrl;
                retArt.Tags = artOfDay.tagLabels;
                
            }
            return retArt;
        }

        public static OAuthor GetAuthorFromBendy(dynamic author)
        {
            OAuthor aut = null;
            if (author != null)
            {
                aut = new OAuthor();
                aut.Name = author.authorName;
                aut.Desc = author.authorDesc;
                aut.Email = author.authorEmail;
                aut.LinkWebsite = author.linkWebsite;
                aut.Link3 = author.authorLink3;
                aut.LinkBlog = author.linkBlogs;
                aut.Twitter = author.twitterHandle;
                aut.Facebook = author.facebookLink;
                aut.GooglePlus = author.googlePlusLink;
            }
            return aut;
        }

        public static OContentProvider GetContentProviderFromBendy(dynamic cprovider)
        {
            OContentProvider ret = null;
            if (cprovider != null)
            {
                ret = new OContentProvider();
                ret.Name = cprovider.providerName;
                ret.Desc = cprovider.providerDesc;
                ret.Email = cprovider.providerEmail;
                ret.LinkWebsite = cprovider.providerWebsite;
                ret.LinkRss = cprovider.providerRssLink;
                ret.Link3 = cprovider.providerLink3;
                ret.Twitter = cprovider.twitterHandle;
                ret.Facebook = cprovider.facebookLink;
                ret.GooglePlus = cprovider.googlePlusLink;

            }
            return ret;
        }


        private static int GetCurrentArticleId()
        {
            int artid = 0;
            string currentDt = DateTime.Now.ToString("yyyyMMdd");
            artid = currentDt.As<int>();
            return artid;
        }
    }
}